package coganh.Activity;

import game.GameThread;
import game.GameView;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import coganh.Activity.R;

public class InGame extends Activity {
	private static final int MENU_STOP = 4;
	private static final int MENU_CLEAR = 5;
	private static final int MENU_PAUSE = 6;
	private static final int MENU_RESUME = 7;
	private GameView mGameView;
	private GameThread mgGameThread;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(0, MENU_CLEAR, 0, R.string.menu_clear);
		menu.add(0, MENU_STOP, 0, R.string.menu_exit);
		menu.add(0, MENU_PAUSE, 0, R.string.menu_pause);
		menu.add(0, MENU_RESUME, 0, R.string.menu_resume);

		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_CLEAR:
			mgGameThread.clearSprites();
			return true;
		case MENU_STOP:
			finish();
			return true;
		case MENU_PAUSE:
			mgGameThread.pause();
			return true;
		case MENU_RESUME:
			mgGameThread.unpause();
			return true;
		}

		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_in_game);

		mGameView = (GameView) findViewById(R.id.game);
		mgGameThread = mGameView.getThread();

		if (savedInstanceState == null) {
			// moi ket noi voi game: new game
			mgGameThread.setState(GameThread.STATE_READY);
			Log.w(this.getClass().getName(), "eo biet bi chi");
		} else {
			// resume game
			mgGameThread.restoreState(savedInstanceState);
			Log.w(this.getClass().getName(), "SIS is nonnull");
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		mGameView.getThread().pause(); // pause game khi Activity pauses
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// just have the View's thread save its state into our Bundle
		super.onSaveInstanceState(outState);
		mgGameThread.saveState(outState);
		Log.w(this.getClass().getName(), "SIS called");
	}
}
